package com.dw.etl.core.service;

import com.dw.etl.common.util.UUIDUtil;
import  com.dw.etl.common.vo.PaginationVO;
import com.dw.etl.core.bean.model.MdMedPubfld;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import com.dw.etl.core.api.MdMedDimService;
import com.dw.etl.core.mapper.MdMedDimMapper;
import com.dw.etl.core.bean.model.MdMedDim;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * mdMedDim服务实现类
 * Created by hozeData on 2021/07/05.
 */
@Service
@Transactional
public class MdMedDimServiceImpl implements MdMedDimService{

    private static final Logger LOGGER = LoggerFactory.getLogger(MdMedDimServiceImpl.class);

    private static Map<String,MdMedDim> MD_MED_DIM_DATAMAP = new HashMap<>();

    @Autowired
    private MdMedDimMapper mdMedDimMapper;

    @Override
    public int insert(MdMedDim mdMedDim) {
        if(StringUtils.isEmpty(mdMedDim.getIdDim())){
            mdMedDim.setIdDim(mdMedDim.getIdPubfld());
        }
        return mdMedDimMapper.insert(mdMedDim);
    }

    @Override
    public int insertSelective(MdMedDim mdMedDim)  {
        if(StringUtils.isEmpty(mdMedDim.getIdDim())){
            mdMedDim.setIdDim(mdMedDim.getIdPubfld());
        }
        return mdMedDimMapper.insertSelective(mdMedDim);
    }

    @Override
    public int deleteById(String idDim)  {
        return mdMedDimMapper.deleteByPrimaryKey(idDim);
    }

    @Override
    public int updateByIdSelective(MdMedDim mdMedDim)  {

        return mdMedDimMapper.updateByPrimaryKeySelective(mdMedDim);
    }

    @Override
    public int updateById(MdMedDim mdMedDim)  {
        return mdMedDimMapper.updateByPrimaryKey(mdMedDim);
    }

    @Override
    public MdMedDim getById(String idDim)  {
        return mdMedDimMapper.selectByPrimaryKey(idDim);
    }

    @Override
    public List<MdMedDim> findByModel(MdMedDim mdMedDim)  {
        return mdMedDimMapper.findByModel(mdMedDim);
    }

    @Override
    public PaginationVO<MdMedDim> findPageByModel(MdMedDim mdMedDim, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum == null? 1 : pageNum, pageSize == null? 10 : pageSize);
        List<MdMedDim> quesList = mdMedDimMapper.findByModel(mdMedDim);
        PageInfo<MdMedDim> pageInfo = new PageInfo<>(quesList);
        PaginationVO<MdMedDim> paginationVo = new PaginationVO(pageInfo.getTotal(), pageInfo.getList());
        return paginationVo;
    }

    @Override
    public PaginationVO<MdMedDim> findPageByLike(String filter, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum == null? 1 : pageNum, pageSize == null? 10 : pageSize);
        List<MdMedDim> quesList = mdMedDimMapper.findByLike(filter);
        PageInfo<MdMedDim> pageInfo = new PageInfo<>(quesList);
        PaginationVO<MdMedDim> paginationVo = new PaginationVO(pageInfo.getTotal(), pageInfo.getList());
        return paginationVo;
    }

    @Override
    public List<MdMedDim> findByIdList(List<String> idList) {
        return mdMedDimMapper.findByIdList(idList);
    }

    @Override
    public String getCdDimgrp(List<String> idList) {
        List<MdMedDim> list = this.findByIdList(idList);
        String cdDimgrp = "";
        for(MdMedDim fld:list) {
            cdDimgrp += fld.getCd()+"_";
        }
        return cdDimgrp.substring(0,cdDimgrp.length()-1);
    }

    @Override
    public MdMedDim getByIdFromCache(String idDim) {
        MdMedDim dim ;
        if((dim=MD_MED_DIM_DATAMAP.get(idDim))==null) {
            dim = this.getById(idDim);
            MD_MED_DIM_DATAMAP.put(idDim,dim);
        }
        return dim;
    }
}
